﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgramMain.Database;
using ProgramMain.Framework;
using ProgramMain.Google;

namespace ProgramMain.OpticNet
{
    public partial class CableTunnelDistanceList : ProgramMain.Framework.BaseGridListCtl
    {
        public CableTunnelDistanceList()
        {
            InitializeComponent();
           
        }

        public void CalcLen(Coordinate oldCoordinate, Coordinate newCoordinate)
        {
            for (int i = 0; i < dataSource.Count; i++)
            {
                dataSource.Position = i;
                SimpleOpticNetDB.CableTunnelsRow row = (SimpleOpticNetDB.CableTunnelsRow)((DataRowView)dataSource.Current).Row;
                if (row != null)
                {
                    if (row.Longitude1 == (decimal)oldCoordinate.Longitude && row.Latitude1 == (decimal)oldCoordinate.Latitude)
                    {
                        row.Length = (int)(new Coordinate(row.Longitude2, row.Latitude2).Distance(newCoordinate));
                    }

                    if (row.Longitude2 == (decimal)oldCoordinate.Longitude && row.Latitude2 == (decimal)oldCoordinate.Latitude)
                    {
                        row.Length = (int)(new Coordinate(row.Longitude1, row.Latitude1).Distance(newCoordinate));
                    }
                }
            }
        }

        public Image RedirectButtonImage
        {
            set { repositoryItemButtonEdit1.Buttons[0].Image = value; }
        }

        public int RedirectVisibleIndex { get; set; }
        private int? _hiddenCableTunnelID;
        public int? HiddenCableTunnelID
        {
            get { return _hiddenCableTunnelID; }
            set
            {
                if(_hiddenCableTunnelID !=value)
                {
                    _hiddenCableTunnelID = value;
                    this.LoadData();
                }
            }
        }

        protected override void LoadData()
        {
            this.cableTunnelsTableAdapter.SelectCommandTimeout =
                ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.cableTunnelsTableAdapter.Fill(simpleOpticNetDB.CableTunnels, null, DetailID, null, null, null, null,
                                               null, null, HiddenCableTunnelID);
            UpdateStatusText(simpleOpticNetDB.CableTunnels);
        }

        private void CableTunnelDistanceList_DetailIDChanged(object sender, Framework.DetailEventArgs e)
        {
            LoadData();
        }
        public event EventHandler<IntEvent> CableTunnelRedirect;
        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            SimpleOpticNetDB.CableTunnelsRow row = (SimpleOpticNetDB.CableTunnelsRow)((DataRowView)dataSource.Current).Row;
            if (row != null)
            {
                if (CableTunnelRedirect != null) CableTunnelRedirect(this, new IntEvent(row.ID));
            }
        }

        private void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
        {

        }

        private void CableTunnelDistanceList_Load(object sender, EventArgs e)
        {
            
        }

        private void gridControl_Load(object sender, EventArgs e)
        {
            gridView.OptionsView.ShowAutoFilterRow = false;
            gridView.OptionsView.ColumnAutoWidth = true;
            colRedirect.VisibleIndex = RedirectVisibleIndex;
        }
    }
}
